package com.web.thread.crawler;

import java.io.IOException;
import java.util.UUID;

import org.apache.commons.lang.SerializationUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.web.model.crawler.TbGrading;
import com.web.model.crawler.TbPcCom;
import com.web.rabbitmq.crawler.Rabbit;
import com.web.util.crawler.GetHtml;

/**
 * 根据分类获取产品列表信息线程
 * 
 * @author G8670
 * 
 */
public class PcComThread extends Thread {

	private TbGrading grading;

	private String QUEUE_NAME;// 产品列表队列频道标识

	private String GRADING_QUEUE_NAME = "grading_end";// 已处理的分类队列频道标识

	private static final Logger logger = Logger.getLogger(PcComThread.class);

	private static String url = "https://www.taobao.com/";

	private static String action_url;

	static {
		Document indet_doc = Jsoup.parse(GetHtml.get(url));// 请求首页，获得首页信息
		Element index_element = indet_doc.getElementById("J_TSearchForm");// 找到搜素框节点Form
		action_url = "https:" + index_element.attr("action");// 找到搜素Form提交的action地址
	}

	public PcComThread(TbGrading grading) {
		this.grading = grading;
	}

	@Override
	public void run() {

		QUEUE_NAME = grading.getName();
		Connection connection = Rabbit.getConnection();// 获取队列连接
		Channel channel = Rabbit.getChannel(connection, QUEUE_NAME);// 获取队列频道

		logger.info(QUEUE_NAME + "***开始处理***");
		addCommodity(grading, channel);

		Connection connection_grading = Rabbit.getConnection();// 获取已处理队列连接
		Channel channel_grading = Rabbit.getChannel(connection_grading, GRADING_QUEUE_NAME);// 已处理获取队列频道
		// 将已处理完的分类存进队列
		try {
			channel_grading.basicPublish("", GRADING_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(grading));
			Rabbit.close(connection_grading, channel_grading);// 关闭已处理完的分类队列连接和频道
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Rabbit.close(connection, channel);// 关闭分类队列连接和频道
	}

	/**
	 * 根据分类抓取产品列表
	 * 
	 * @param grading
	 *            分类对象
	 * @param searchName
	 * @param channel
	 */
	private void addCommodity(TbGrading grading, Channel channel) {
		String searchName = grading.getName();
		int p_num = 0;// 控制页码，每页显示44条记录，逐次累加即可实现翻页
		while (true) {
			try {
				String search_url = action_url + "?q=" + searchName + "&s=" + p_num;
				// System.out.println(get(search_url));

				Document search_doc = Jsoup.parse(GetHtml.get(search_url));// 查询信息
				Elements els = search_doc.select("script");// 获取查询页js

				String str = els.get(5).data();// 获取js中g_page_config数据（当前页面的产品展示信息）并处理成json所需的字符串格式
				String str_json = "{" + '"' + str.substring(str.indexOf("itemlist"), str.indexOf("bottomsearch"));
				str_json = str_json.substring(0, str_json.length() - 1) + "}";
				// System.out.println(str_json);

				JSONObject jsonObect = (JSONObject) JSON.parseObject(str_json);// 将拼接的字符串转成json对象
				// 逐层获取得到产品信息JSONArray
				JSONObject itemlist_json = jsonObect.getJSONObject("itemlist");
				JSONObject data_json = itemlist_json.getJSONObject("data");
				JSONArray auctions_array = data_json.getJSONArray("auctions");
				// System.out.println(auctions_array.size());

				for (int i = 0; i < auctions_array.size(); i++) {
					TbPcCom tbCom = new TbPcCom();// 产品类model对象

					JSONObject obj = (JSONObject) auctions_array.get(i);
					tbCom.setId(UUID.randomUUID().toString());
					tbCom.setName(obj.get("raw_title").toString());// 产品名称
					tbCom.setPicUrl(obj.get("pic_url").toString());// 产品图片url
					tbCom.setHref(obj.get("detail_url").toString());// 产品url
					tbCom.setCurrentPrice(obj.get("view_price").toString());// 产品价格
					tbCom.setPrice(obj.get("reserve_price").toString());// 产品原价格
					tbCom.setTradeNum(obj.get("view_sales").toString());// 销量（例：12人付款）
					tbCom.setNick(obj.get("nick").toString());// 店铺名称
					tbCom.setItemLoc(obj.get("item_loc").toString());// 店铺地址
					tbCom.setTbGrading(grading);// 所属分类对象
					// PcCommodityService.save(tbCom);
					try {
						// 存进队列
						channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(tbCom));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				// System.out.println(p_num);
				p_num += 44;// 翻页
			} catch (NullPointerException e) {
				continue;// 处理过程出现异常，结束本次循环，执行下一次
			} catch (IndexOutOfBoundsException e) {
				TbPcCom tbCom = new TbPcCom();// 产品类model对象
				tbCom.setId("-1");
				try {
					// 结束标记
					channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, SerializationUtils.serialize(tbCom));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				logger.info(searchName + "***处理完毕***");
				break;// 没有当前产品的数据，跳出
			}
		}

	}
}
